package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import tiendavirtual.negocio.GestionCategoria;
import tiendavirtual.exception.DAOExcepcion;
import tiendavirtual.negocio.GestionCategoria;
import tiendavirtual.negocio.GestionSubCategoria;

/**
 * Servlet implementation class Servlet_RegistrarCategoria
 */
@WebServlet("/Servlet_RegistrarCategoria")
public class Servlet_RegistrarCategoria extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet_RegistrarCategoria() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 response.setContentType("text/html");
		 PrintWriter out = response.getWriter();
		
		
		
		String nombre_categoria = request.getParameter("txtCategoria");
		String descripcion_categoria=request.getParameter("txtDescripcionCat");
		
		
		request.setAttribute("txtCategoria", nombre_categoria);
		request.setAttribute("txtDescripcionCat", descripcion_categoria);
		
		 out.println("<html>" + 
                 "<head>" + 
                 "<body>Se inserto correctamente</body></html>");
		 out.close();
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String x = request.getParameter("txtCategoria");
		String y = request.getParameter("txtDescripcionCat");
		
		GestionCategoria negocio = new GestionCategoria();
		try {
			negocio.insertar(x, y);
			response.sendRedirect(request.getContextPath()	+ "/Servlet_RegistrarCategoria");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error_categoria.jsp");
			rd.forward(request, response);
		}
		
		
		
		
		

	}
		

		
		
	

}
